package JAVA300.第十章IO;

/**
 * 理解装饰器模式成分--借助咖啡实例
 * 1.抽象组建
 * 2.具体组建
 * 3.抽象装饰类
 * 4.具体装饰类
 */

public class IO_Study09_decorateDemo2 {
    public static void main(String[] args) {
        Drink coffee = new Coffee();
        Drink sugar = new Sugar(coffee);
        System.out.println(sugar.info()+"-->"+sugar.price());//"原味咖啡sugar-->30.0"
        Drink milk = new Milk(coffee);
        System.out.println(milk.info()+"-->"+milk.price());//原味咖啡milk-->20.0
        Drink milk1 = new Milk(sugar);
        System.out.println(milk1.info()+"-->"+milk1.price());//60=10*3*2


    }

}
//1,抽象组建
interface Drink{
    double price();//价格
    String info();//说明

}
//具体组建
class Coffee implements Drink{
    private String name ="原味咖啡";

    @Override
    public double price() {
        return 10;
    }

    @Override
    public String info() {
        return name;
    }
}

//抽象装饰
abstract class Decorate implements Drink{

    //抽象组建引用
    Drink drink;

    public Decorate(Drink drink) {
        this.drink = drink;
    }

    @Override
    public double price() {
        return this.drink.price();
    }

    @Override
    public String info() {
        return this.drink.info();
    }
}

class Milk extends Decorate{

    public Milk(Drink drink) {
        super(drink);
    }

    @Override
    public double price() {
        return super.price()*2;
    }

    @Override
    public String info() {
        return super.info()+"add milk";
    }
}

class Sugar extends Decorate{

    public Sugar(Drink drink) {
        super(drink);
    }

    @Override
    public double price() {
        return super.price()*3;
    }

    @Override
    public String info() {
        return super.info()+"add sugar";
    }
}




